package com.smm.web.vmc.api;

import com.smm.web.vmc.domain.Alam;
import com.smm.web.vmc.domain.Pager;
import com.smm.web.vmc.dto.JsonReturn;
import com.smm.web.vmc.service.AlamService;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang3.StringUtils;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.annotation.Resource;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * Created by zhenghao on 2016/9/29.
 *
 */
@Slf4j
@Controller
@RequestMapping(value = "/api/alam", method = RequestMethod.POST)
public class AlamAPI {

    @Resource
    private AlamService alamService;


    //接收报警接口， 第三方收到报警后推送仓单信息
    @RequestMapping(value = "recive" ,method = RequestMethod.GET)
    @ResponseBody
    public JsonReturn recive(@RequestParam String repNo){

        try{

            if(StringUtils.isBlank(repNo)) throw new Exception("仓单编号不能为空");

            //执行报警抓拍
            this.alamService.recive(repNo);

            return new JsonReturn(1,"接收报警信息成功");

        }catch (Exception e){

            log.error(String.valueOf(e));

            return new JsonReturn(0,e.getMessage());

        }

    }


    @RequestMapping("/listbyuser.do")
    @ResponseBody
    public JsonReturn listByUser(int userId,int pageNo){

        Pager  pager = new Pager(pageNo);

        List<Alam> alamList = alamService.getPagerAlamListByUser(userId, pager);

        if(alamList==null) alamList = new ArrayList<>();

        //过滤不需要的数据
        alamList = filterData(alamList);

        Map<String ,Object> result = new HashMap<>();

        result.put("list",alamList);

        result.put("pager",pager);

        return new JsonReturn(1,"success",result);
    }



    @RequestMapping("/info.do")
    @ResponseBody
    public JsonReturn info(int id){

        Alam alam = alamService.getOneById(id);

        return new JsonReturn(1,"success",alam);
    }




    //过滤不需要的属性值
    private List<Alam> filterData(List<Alam> alamList) {

        List<Alam> resultList = new ArrayList<>();

        for(Alam alam:alamList){

//            alam.setReceipt(null);
            alam.setTag(null);

            resultList.add(alam);
        }

        return resultList;
    }
}
